package com.briup.day16;

import java.util.*;

public class Test_HashMap02 {
    public static void main(String[] args) {
        //HashTable实现类，键和值不可以为null
        Hashtable<Student,String> hashtable=new Hashtable<Student,String>();
        hashtable.put(new Student("zs", 78), "010");
        hashtable.put(new Student("rose", 82), "005");
        hashtable.put(new Student("lucy", 70), "009");
        hashtable.put(new Student("lucy", 70), "019"); // 相同key，只能保留一项，"019"会覆盖"009"
        hashtable.put(new Student("ww", 67), "002");
        // 3.遍历，Hashtable早期提供的方法较为繁琐
        Enumeration<Student> keys = hashtable.keys();
        while(keys.hasMoreElements()) {
            Student key = keys.nextElement();
            String value = hashtable.get(key);
            System.out.println(key + ": " + value);
        }
    }
    public static void main01(String[] args) {
        Map<Student, String> map = new HashMap<>();
        //判断键和值是否存在
        //一但hashcode将自定义类作为键
        //一定要重写equals和hashcode方法
        //        为什么？HashMap的工作原理：
        //HashMap通过两个步骤判断key是否相同：
        //1. 先比较 hashCode()
        //如果hashCode不同 → 直接认为是不同的key
        //如果hashCode相同 → 进入第2步
        //2. 再比较 equals()
        //如果equals返回true → 认为是同一个key，覆盖value
        //如果equals返回false → 认为是不同的key，形成链表
        map.put(new Student("zs", 78), "010");
        map.put(new Student("rose", 82), "005");
        map.put(new Student("lucy", 70), "009");
        map.put(new Student("lucy", 70), "019"); //相同key，只能保留一项，"019"会覆盖"009"
        map.put(new Student("ww", 67), "002");
        //注意：HashMap中key和value都可以为null
        map.put(new Student("tom", 86), null);
        map.put(null, "002");
        System.out.println(map);
        System.out.println(map.size());
        Set<Student> set=map.keySet();
        System.out.println(set);
        for(Student key:set){
            System.out.println(map.get(key));
            System.out.println("键"+key+"值"+map.get(key));
        }
        System.out.println("--------------------------------");
        boolean key = map.containsKey(new Student("lucy",70));
        System.out.println(key);
        boolean value = map.containsValue("009");
        System.out.println(value);
        String f = map.remove(new Student("lucy",70));
        System.out.println(f);
        System.out.println("---------------------------------");
        Set<Student> students = map.keySet();
        for(Student key1:students){
            System.out.println(map.get(key1));
        }
        Set<Map.Entry<Student,String>> m= map.entrySet();
        for(Map.Entry<Student,String> entry:m){
            System.out.println(entry);

        }
    }
}

class Student {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Student)) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
